<template>
  <view class="review-container">
    <!-- 筛选栏 -->
    <view class="filter-bar">
      <view class="filter-item" @click="showSortPicker">
        <text class="filter-text">{{ sortText }}</text>
        <text class="filter-icon">▼</text>
      </view>
      <picker
        class="filter-item"
        mode="date"
        :value="currentDate"
        @change="onDateChange"
      >
        <text class="filter-text">{{ dateText }}</text>
        <text class="filter-icon">▼</text>
      </picker>
    </view>

    <!-- 处方数量 -->
    <view class="prescription-count">共计{{ prescriptionList.length }}张</view>
    <!-- 处方列表 -->
    <scroll-view class="prescription-list" scroll-y>
      <view
        v-for="(item, index) in prescriptionList"
        :key="index"
        class="prescription-item"
      >
        <view class="item-content">
          <view class="item-header">
            <text class="label">处方单号：</text>
            <text class="value">{{ item.prescriptionNo }}</text>
            <view
              v-if="isDowngraded(index)"
              class="audit-status"
              :class="
                getDowngradeStatus(index) === '已通过'
                  ? 'status-pass'
                  : 'status-reject'
              "
            >
              <text class="status-text">{{ getDowngradeStatus(index) }}</text>
            </view>
          </view>
          <view class="item-row">
            <text class="label">开方科室：</text>
            <text class="value">{{ item.department }}</text>
          </view>
          <view class="item-row">
            <text class="label">患者姓名：</text>
            <text class="value">{{ item.patientName }}</text>
          </view>
          <view class="item-row">
            <text class="label">发送时间：</text>
            <text class="value">{{ item.sendTime }}</text>
          </view>
          <view class="item-row">
            <text class="label">待降级药品：</text>
            <text class="value">{{ item.specialDrug }}</text>
          </view>
        </view>
        <view class="item-footer" v-if="!isDowngraded(index)">
          <button class="review-btn" @click="handleReview(item, index)">
            降级确认
          </button>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      sortText: "默认排序",
      dateText: "日期筛选",
      currentDate: "",
      sortOptions: ["不限", "由低到高", "由高到低"],
      downgradeList: [], // 已降级列表
      prescriptionList: [
        {
          prescriptionNo: "MZ20250120001",
          department: "骨科",
          patientName: "刘建华",
          sendTime: "2025-01-20 09:15:30",
          specialDrug: "塞来昔布胶囊（特殊级抗炎药）",
        },
        {
          prescriptionNo: "MZ20250120002",
          department: "风湿免疫科",
          patientName: "孙美玲",
          sendTime: "2025-01-20 10:30:45",
          specialDrug: "甲氨蝶呤片（抗肿瘤药）",
        },
        {
          prescriptionNo: "MZ20250120003",
          department: "神经外科",
          patientName: "周天明",
          sendTime: "2025-01-20 11:20:15",
          specialDrug: "加巴喷丁胶囊（限制级镇痛药）",
        },
        {
          prescriptionNo: "MZ20250121004",
          department: "肿瘤内科",
          patientName: "吴秀英",
          sendTime: "2025-01-21 14:45:20",
          specialDrug: "伊马替尼片（靶向治疗药物）",
        },
        {
          prescriptionNo: "MZ20250121005",
          department: "精神科",
          patientName: "郑国强",
          sendTime: "2025-01-21 15:30:50",
          specialDrug: "奥氮平片（抗精神病药）",
        },
        {
          prescriptionNo: "MZ20250122006",
          department: "血液内科",
          patientName: "冯丽丽",
          sendTime: "2025-01-22 08:50:10",
          specialDrug: "华法林钠片（抗凝血药）",
        },
        {
          prescriptionNo: "MZ20250122007",
          department: "皮肤科",
          patientName: "黄建军",
          sendTime: "2025-01-22 16:40:25",
          specialDrug: "异维A酸软胶囊（限制级皮肤用药）",
        },
      ],
    };
  },
  onLoad() {
    // 检查登录状态
    const isLogin = uni.getStorageSync("isLogin");
    if (!isLogin) {
      uni.reLaunch({
        url: "/pages/login/login",
      });
      return;
    }

    // 初始化当前日期
    const date = new Date();
    this.currentDate = `${date.getFullYear()}-${String(
      date.getMonth() + 1
    ).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;

    // 加载数据
    this.loadData();
  },
  onShow() {
    // 每次显示页面时重新加载降级状态
    this.loadDowngradeList();
  },
  methods: {
    // 加载数据
    loadData() {
      // 这里可以调用后端接口获取实际数据
      // 暂时使用模拟数据
    },

    // 加载已降级列表
    loadDowngradeList() {
      this.downgradeList = uni.getStorageSync("downgradeList") || [];
    },

    // 判断是否已降级
    isDowngraded(index) {
      return this.downgradeList.some((item) => item.index === index);
    },

    // 获取降级状态文本
    getDowngradeStatus(index) {
      const downgrade = this.downgradeList.find((item) => item.index === index);
      if (!downgrade) return "";
      return downgrade.result === "pass" ? "已通过" : "未通过";
    },

    // 显示排序选择器
    showSortPicker() {
      uni.showActionSheet({
        itemList: this.sortOptions,
        success: (res) => {
          this.sortText = this.sortOptions[res.tapIndex];
          this.applySort(res.tapIndex);
        },
      });
    },

    // 日期变更
    onDateChange(e) {
      const selectedDate = e.detail.value;
      this.dateText = selectedDate;
      this.currentDate = selectedDate;
      this.applyDateFilter(selectedDate);
    },

    // 应用排序
    applySort(sortType) {
      // 这里实现排序逻辑
      uni.showToast({
        title: `已切换到${this.sortText}`,
        icon: "none",
      });
    },

    // 应用日期筛选
    applyDateFilter(date) {
      // 这里实现日期筛选逻辑
      uni.showToast({
        title: `筛选日期：${date}`,
        icon: "none",
      });
    },

    // 降级确认处理
    handleReview(item, index) {
      // 跳转到通用详情页 - 降级确认模式
      const data = {
        prescription: item,
        index: index,
      };
      uni.navigateTo({
        url: `/pages/detail/detail?type=downgrade&source=outpatient&data=${encodeURIComponent(
          JSON.stringify(data)
        )}`,
      });
    },
  },
};
</script>

<style scoped>
.review-container {
  height: calc(100vh - 85rpx);
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 筛选栏 */
.filter-bar {
  display: flex;
  background-color: #ffffff;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.filter-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20rpx;
  border-right: 1rpx solid #eeeeee;
}

.filter-item:last-child {
  border-right: none;
}

.filter-text {
  font-size: 28rpx;
  color: #333333;
  margin-right: 10rpx;
}

.filter-icon {
  font-size: 20rpx;
  color: #999999;
}

/* 处方列表 */
.prescription-count {
  font-size: 30rpx;
  color: #232323;
  padding: 15rpx 30rpx 0;
}

.prescription-list {
  width: calc(100% - 60rpx);
  flex: 1;
  padding: 20rpx 30rpx;
  overflow-y: auto;
}

.prescription-item {
  background-color: #ffffff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  position: relative;
  padding-bottom: 80rpx;
}

.item-content {
  width: 100%;
}

.item-header {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #eeeeee;
}

/* 审核状态 */
.audit-status {
  margin-left: auto;
  padding: 6rpx 20rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
}

.status-pass {
  background-color: #e8f5e9;
  color: #4caf50;
}

.status-reject {
  background-color: #ffebee;
  color: #f44336;
}

.status-text {
  font-size: 24rpx;
}

.item-row {
  display: flex;
  margin-bottom: 16rpx;
}

.label {
  font-size: 28rpx;
  color: #666666;
  min-width: 145rpx;
}

.value {
  font-size: 28rpx;
  color: #333333;
  flex: 1;
}

.item-footer {
  position: absolute;
  bottom: 20rpx;
  right: 30rpx;
}

.review-btn {
  background-color: #4375fb;
  color: #ffffff;
  font-size: 26rpx;
  padding: 8rpx 36rpx;
  border-radius: 6rpx;
  border: none;
  line-height: 1.4;
  display: flex;
  align-items: center;
  justify-content: center;
}

.review-btn::after {
  border: none;
}
</style>
